package com.axun.qqcountdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by hz-java on 2018/9/29.
 */

public class UserManageAdapter extends BaseAdapter {

    private Context context;
    private List<UserSavedModule> items;

    private boolean isChange = false;

    private int selection =0;

    private int showDelete = -1;

    public UserManageAdapter(Context context) {
        this.context = context;
    }

    public void setItems(List<UserSavedModule> items) {
        this.items = items;
        notifyDataSetChanged();
    }

    public void setChange(boolean change) {
        isChange = change;
        showDelete = -1;
        notifyDataSetChanged();
    }

    public void setSelection(int selection) {
        if (!isChange){
            this.selection = selection;
            notifyDataSetChanged();
        }
    }

    public void setShowDelete(int showDelete) {
        this.showDelete = showDelete;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public Object getItem(int position) {
        return items == null ? null : items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return items == null ? 0 : position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final UserHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_user_list, parent, false);
            holder = new UserHolder();
            holder.mIvDelete = (ImageView) convertView.findViewById(R.id.iv_delete);
            holder.mIvUser = (ImageView) convertView.findViewById(R.id.iv_user);
            holder.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
            holder.mTvPhone = (TextView) convertView.findViewById(R.id.tv_phone);
            holder.mIvCheck = (ImageView) convertView.findViewById(R.id.iv_check);
            holder.mTvDelete = (TextView) convertView.findViewById(R.id.tv_delete);
            convertView.setTag(holder);
        }else {
            holder = (UserHolder) convertView.getTag();
        }
        holder.mTvName.setText(items.get(position).getName());
        holder.mTvPhone.setText(items.get(position).getPhone());
        if (isChange){
            //编辑状态
            if (holder.mIvDelete.getVisibility()!=View.VISIBLE){

                MuAniUtils.hideWithAlpha(holder.mIvCheck,200);
                MuAniUtils.showWithAlpha(holder.mIvDelete,200);
            }

            if (showDelete == position){
               if (holder.mTvDelete.getVisibility()==View.VISIBLE){
                   MuAniUtils.resetRotateView(holder.mIvDelete,200);
                   MuAniUtils.hideWithWidth(holder.mTvDelete,200);
               }else {
                   MuAniUtils.rotateView(holder.mIvDelete,200);
                   MuAniUtils.showWithWidth(holder.mTvDelete,200);
//                   holder.mTvDelete.setVisibility(View.VISIBLE);
               }
            }else {
               if (holder.mTvDelete.getVisibility()==View.VISIBLE){
                   MuAniUtils.resetRotateView(holder.mIvDelete,200);
                   MuAniUtils.hideWithWidth(holder.mTvDelete,200);
               }else {
                   MuAniUtils.resetRotateView(holder.mIvDelete,0);
               }
            }

            holder.mIvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   if (listener!=null){
                       listener.onDeleteIconClick(v,position);
                   }
                }
            });

            holder.mTvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener!=null){
                        listener.onDeleteClick(v,position);
                    }
                }
            });
        }else {
            //普通状态
            MuAniUtils.hideWithAlpha(holder.mIvDelete,200);
            MuAniUtils.showWithAlpha(holder.mIvCheck,200);
            MuAniUtils.hideWithWidth(holder.mTvDelete,200);
            if (selection == position){
               holder.mIvCheck.setVisibility(View.VISIBLE);
            }else {
                holder.mIvCheck.setVisibility(View.GONE);
            }
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.onItemClick(v,position);
                }
            }
        });
        return convertView;
    }



    class UserHolder {
        ImageView mIvDelete;
        ImageView mIvUser;
        TextView mTvName;
        TextView mTvPhone;
        ImageView mIvCheck;
        TextView mTvDelete;
    }

    private OnUserManageListener listener;

    public void setOnUserManageListener(OnUserManageListener listener) {
        this.listener = listener;
    }

    public interface OnUserManageListener{
        void onItemClick(View view,int position);
        void onDeleteIconClick(View view,int position);
        void onDeleteClick(View view,int position);
    }


}
